package com.jushu.storbox.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.operation.group.TargetSearchOperation;
import com.jushu.storbox.util.PublicTools;

public class TargetSearchDisplayAdapter extends
		ListDisplayResolveAdapter<FileFolderInfo, TargetSearchOperation<FileFolderInfo>> {
	
	Context context;
	private LayoutInflater mInflater;
	
	public TargetSearchDisplayAdapter(Context context, List<FileFolderInfo> datasList) {
		super(datasList);
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null) {
			holder = new ViewHolder(mInflater, parent);
			convertView = holder.baseView;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.bindingData(position, getItem(position));
		return convertView;
	}

	public class ViewHolder {
		//ui view
		View baseView;
		ImageView mItemIcon;
		TextView mItemName;
		TextView mItemDate;
		TextView mItemSize;
		CheckBox mItemCheckBox;
		View mItemMore;
		
		//data index
		int mIndex;
		//data
		FileFolderInfo mInfo;
		
		public FileFolderInfo getmInfo() {
			return mInfo;
		}

		public ViewHolder(LayoutInflater mInflater, ViewGroup parent) {
			//init view
			baseView = mInflater.inflate(R.layout.new_item_filelist, parent, false);
			mItemIcon = (ImageView) baseView.findViewById(R.id.item_type_img);
			mItemName = (TextView) baseView.findViewById(R.id.item_name_text);
			mItemDate = (TextView) baseView.findViewById(R.id.item_date_text);
			mItemSize = (TextView) baseView.findViewById(R.id.item_filelist_file_size);
			mItemCheckBox = (CheckBox) baseView.findViewById(R.id.chx_file_item);
			mItemMore = baseView.findViewById(R.id.item_more_ll);
			mItemMore.setVisibility(View.INVISIBLE);
			mItemCheckBox.setVisibility(View.GONE);
		}
		
		public void bindingData(int index, FileFolderInfo info) {
			mIndex = index;
			mInfo = info;
			//bindding data
			mItemIcon.setImageResource(getTypeImgeID());
			mItemName.setText(info.getName());
			if(mInfo.getIsFile() == FileType.File.value()) {
				mItemDate.setVisibility(View.VISIBLE);
				mItemDate.setText(formatDate());
				mItemSize.setVisibility(View.VISIBLE);
				mItemSize.setText(PublicTools.changeBKM(String.valueOf(mInfo.getSize())));
			} else {
				mItemDate.setVisibility(View.VISIBLE);
				mItemDate.setText(formatDate());
				mItemSize.setVisibility(View.INVISIBLE);
			}
		}
		
		@SuppressLint("SimpleDateFormat")
		private String formatDate() {
			SimpleDateFormat timeDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            return timeDateFormat.format(mInfo.getModifiedAt());
		}
		
		private int getTypeImgeID() {
			int typeImgResId = R.mipmap.default_file_icon;
			if(mInfo.getIsFile() == FileType.File.value()) {
				if (PublicTools.isFileType(mInfo.getName(), Constant.WORD_TYPE)) {
					typeImgResId = R.mipmap.doc;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.PPT_TYPE)) {
					typeImgResId = R.mipmap.ppt;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.IMAGE_TYPE)) {
					typeImgResId = R.mipmap.empty_pic;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.RAR_TYPE)) {
					typeImgResId = R.mipmap.rar;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.ZIP_TYPE)) {
					typeImgResId = R.mipmap.rar;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.VIDEO_TYPE)) {
					typeImgResId = R.mipmap.video;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.MUSIC_TYPE)) {
					typeImgResId = R.mipmap.music;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.TXT_TYPE)) {
					typeImgResId = R.mipmap.txt;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.PDF_TYPE)) {
					typeImgResId = R.mipmap.pdf;
				} else if (PublicTools.isFileType(mInfo.getName(), Constant.EXCEL_TYPE)) {
					typeImgResId = R.mipmap.excel;
				} else {
					typeImgResId = R.mipmap.default_file_icon;
				}
			} else {
				typeImgResId = R.mipmap.folder_im;
			}
			return typeImgResId;
		}
		
	}
}
